
<template>
    <div class="formmain">
    <el-checkbox v-model="currentValue" >{{label}}</el-checkbox>
    
</div>
    
    
</template>
 
<script>
    export default {
        props:['dataSource','value','label','name','data','id','isreadonly','exkey'],
        name: "input-checkbox",
        data(){
            return {
                currentValue:this.value,
                pId:"",
                cId:"",
                itemData:[],
                isFirst:"Y"
            }
        },
        created(){
            let ids=this.id.split(".");
            if(ids.length>1){
                let p=ids[0];
                let id_=ids[1];
                this.pId=p;
                this.cId=id_;
              //  this.currentValue=this.data[p][id_]=="Y"||this.data[p][id_]=="是" ? true:false;
                this.currentValue=unescape(this.data[p][id_]);
                if(this.currentValue=="Y"){
                    this.currentValue=true;
                }else
                    this.currentValue=false;
            }
            
        },
        methods:{
            initData(){
               // console.log("inputCheckBox=========="+this.currentValue)
                if(this.currentValue==true||this.currentValue=="Y"){
                    if(this.exkey&&this.exkey.setShowField){
                            let setShowField=this.exkey.setShowField;
                            setShowField.forEach(item=>{
                               // this.data.show[item.id]='N'
                                this.$set(this.data.show,item.id,"N")
                            })
                            
                        }
                }else{
                    if(this.exkey&&this.exkey.setShowField){
                            let setShowField=this.exkey.setShowField;
                            setShowField.forEach(item=>{
                               // this.data.show[item.id]='N'
                                this.$set(this.data.show,item.id,"Y")
                            })
                            
                        }
                }
            }
        },
        watch:{
            currentValue: function (val) { //currentValue值变动就向父组件传值
                console.log("inputCheckbox===================")
                let data={};
                data.id=this.id;
                let vals="";
                console.log("checkbox=="+this.id+"=="+this.isFirst+"=="+val)
                if(val==true){
                    data.val="Y";
                    vals="Y";
                    try{
                        //添加服务项目
                        if(this.exkey&&this.exkey.setItem&&this.isFirst!='Y'){
                            for(let i=0;i<this.exkey.setItem.length;i++){
                                let name=this.exkey.setItem[i].name;
                                let itemName=this.exkey.setItem[i].itemName;
                                let dataField=this.exkey.setItem[i].dataField;
                                let exField=this.exkey.setItem[i].exField;
                                let isOk=true;
                                for(let n=0;n<this.data[dataField].length;n++){
                                    let szi_ex1=this.data[dataField][n].szi_ex1;
                                    if(szi_ex1==name){
                                        isOk=false;
                                    }
                                }
                                if(isOk){
                                    if(itemName){
                                        this.itemData=this.data[itemName];
                                        for(let n=0;n<this.itemData.length;n++){
                                            let szi_ex1=this.itemData[n].szi_ex1;
                                            if(szi_ex1==name){
                                                this.$set(this.data[dataField],this.data[dataField].length,this.itemData[n])
                                            }
                                        }
                                        
                                    }else{
                                        let datas={}
                                        datas.addType="string";
                                        datas.itemType="";
                                        datas.nameType="";
                                        datas.val=name;
                                        datas.hour="1";
                                        datas.day="1";
                                    
                                        this.$emit("funeralItemSelectEvent",JSON.stringify(datas))
                                    }
                                }
                                
                                
                            }
                        }

                        if(this.exkey&&this.exkey.setShowField){
                            let setShowField=this.exkey.setShowField;
                            setShowField.forEach(item=>{
                               // this.data.show[item.id]='N'
                                this.$set(this.data.show,item.id,"N")
                            })
                            
                        }
                    }catch(e){}
                }else{
                    vals="N";
                    data.val="N";
                    //删除附加项目
                    if(this.exkey&&this.exkey.setItem&&this.isFirst!='Y'){
                        for(let i=0;i<this.exkey.setItem.length;i++){
                            let name=this.exkey.setItem[i].name;
                            let dataField=this.exkey.setItem[i].dataField;
                            let exField=this.exkey.setItem[i].exField;
                            let pId="";
                            let cId="";
                            let exVal=false;
                            try{
                                if(exField){
                                    let ids=exField.split(".");
                                    pId=ids[0];
                                    cId=ids[1];
                                    exVal=this.data[pId][cId]=='Y';
                                }
                            }catch(e){}
                            for(let h=0;h<this.data[dataField].length;h++){
                                let szi_ex1=this.data[dataField][h].szi_ex1;
                                if(name==szi_ex1&&!exVal){
                                    this.$delete(this.data[dataField],h);
                                    break;
                                }
                            }
                        }
                    }
                    console.log(this.exkey.setShowField,"setShowField======================")
                    if(this.exkey&&this.exkey.setShowField){
                        let setShowField=this.exkey.setShowField;
                        setShowField.forEach(item=>{
                            console.log(item.id,"item.id==============")
                            //this.data.show[item.id]='Y'
                            this.$set(this.data.show,item.id,"Y")
                        })
                        
                    }
                    console.log(this.data.show,"sho====")
                }
                this.$set(this.data[this.pId],this.cId,vals);
                this.isFirst='N'
                //this.$emit('input',JSON.stringify(data));
                //this.$emit('on-change',JSON.stringify(data));
            },
            value:function (val) { //对值进行验证
                
            }
        }
    }
</script>
<style scoped>
.formmain>>>.el-checkbox__inner{
    
    border:1px solid #5c5b5b !important ;
}
</style>
